package com.example.mcmurphy.startproject_115wxy.model;

import com.example.mcmurphy.startproject_115wxy.Iface.WeatherListener;
import com.example.mcmurphy.startproject_115wxy.Iface.Weatheriface;
import com.example.mcmurphy.startproject_115wxy.Service.Weatherservice;
import com.example.mcmurphy.startproject_115wxy.bean.Weatherresult;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * Created by Mcmurphy on 2017/3/14.
 */

public class Weathermodel implements Weatheriface{
    private Retrofit retrofit;
    private String BASEURL="http://www.weather.com.cn/";
    private Weatherservice weatherservice;
    public Weathermodel()
    {
        retrofit=new Retrofit.Builder()
                .baseUrl(BASEURL)
                .addConverterFactory(GsonConverterFactory.create())
                .build();
    }
    @Override
    public void getWeather(String cityno, final WeatherListener listener) {
    weatherservice=retrofit.create(Weatherservice.class);
        Call<Weatherresult>  call=weatherservice.getResult(cityno);
        call.enqueue(new Callback<Weatherresult>() {
            @Override
            public void onResponse(Call<Weatherresult> call, Response<Weatherresult> response) {
                if(response.isSuccessful() && response.body()!=null)
                    listener.onResponse(response.body().getWeatherinfo());
                else
                    listener.onFail("onresponse.fall");
            }

            @Override
            public void onFailure(Call<Weatherresult> call, Throwable t) {
                listener.onFail(t.toString());
            }
        });
    }
}
